package com.lushu.chapitre1.dialogue
{
	import com.lushu.chapitre1.dialogue.api.INode;
	import com.lushu.chapitre1.dialogue.nodes.ActionNode;
	import com.lushu.chapitre1.events.NPCEvent;
    import com.lushu.chapitre1.graphics.controls.TextBox;
    import com.sfdk.io.GenericIO;

    public class DialogueDescriptor extends GenericIO
	{
		private var _dialogueNode : INode;
        private var _dialogueView : TextBox;

        public function DialogueDescriptor(dialogueView : TextBox, filePath : String = null, xmlData : XML = null )
		{
			_dialogueView = dialogueView;

			if(filePath)
				super(filePath);
			else
			{
				_dialogueNode = NodeFactory.getDialogueFromXML(xmlData);
				startDialogue();
			}
			
			_dialogueView.addEventListener(NPCEvent.NEXT_PAGE, onNextPage);
			_dialogueView.addEventListener(NPCEvent.PLAYER_ANSWER, onPlayerAnswerChoosed);
		}

        protected override function init() : void
		{
			_dialogueNode = NodeFactory.getDialogueFromXML(_fileInfo);
			startDialogue();
		}
		
		private var _hasAnswers : Boolean;
		private var _currentTextActions : Vector.<INode>;
		private var _currentNode : INode;
		private var _currentNodeChildIndex : int;
		private var _currentAnswerBuffer : Vector.<INode>;
		private var _currentAnswerBufferActions : Vector.<Vector.<INode>>;
		
		public function startDialogue() : void
		{
			_currentNode = _dialogueNode.children[0];
			parseDialogue();
		}
		
		public function parseDialogue() : void
		{
			if(!_hasAnswers)
			{
				if(_currentTextActions != null)
				{
					parseAction(_currentTextActions);
					_currentTextActions = null;
				}
				
				var nextDialCalled:int=0;
				var debutReponse:int=0;
				
				if(_currentNode.children.length == 0 || _currentNodeChildIndex >= _currentNode.children.length )
				{
					dispatchEvent(new NPCEvent(NPCEvent.END));
				}
				else
				{
					var currentNodeChildren : Vector.<INode> = _currentNode.children;
					var currentNodeChildrenLength : int = currentNodeChildren.length;
					var currentChild : INode = currentNodeChildren[_currentNodeChildIndex];
					if(currentChild == null)
					{
						dispatchEvent(new NPCEvent(NPCEvent.END));
					}
					var nextChild : INode = null;
					if(_currentNodeChildIndex != (currentNodeChildrenLength - 1))
						nextChild = currentNodeChildren[_currentNodeChildIndex + 1];
					if(_currentNode.type == "npc")
					{
						if(currentChild.type == "text" || currentChild.type == "cText")
						{
							if(nextChild != null && nextChild.type == "joueur")
							{
								_currentAnswerBuffer = new Vector.<INode>();
								//_dialogueView.clearPossibleAction();
								
								var nextChildIsJoueur : Boolean = true;
								var i : int = _currentNodeChildIndex + 1;
								var tmpChild : INode;
								
								for(; i < currentNodeChildrenLength; i++)
								{
									// Comme ce sont des joueurs ils ne devraient avoir qu'un textNode / complexTextNode
									tmpChild = currentNodeChildren[i];
									_currentAnswerBuffer.push(tmpChild);
									_hasAnswers = true;
									if(tmpChild.type == "cText")
									{
										//_dialogueView.addPossibleAction(parseText(tmpChild.children[0].children[0].data));
										_currentAnswerBufferActions[_currentAnswerBuffer.length - 1] = tmpChild.children[0].children.concat().slice(1);
									}
									else
									{
										//_dialogueView.addPossibleAction(parseText(tmpChild.children[0].data));
									}
								}	
							}
							else
							{
								_currentNodeChildIndex++;							
							}
							if(currentChild.type == "cText")
							{
								_dialogueView.setContentText(parseText(currentChild.children[0].data), nextChild != null);
								
								_currentTextActions = new Vector.<INode>();
								_currentTextActions = currentChild.children.concat().slice(1);
							}
							else
								_dialogueView.setContentText(parseText(currentChild.data), nextChild != null);
						}
						else if(currentChild.type == "npc")
						{
							_currentNode = currentChild;
							_currentNodeChildIndex = 0;
							parseDialogue();
						}
					}
				}
			}
		}
		
		private function parseAction(actionArray : Vector.<INode>) : void
		{
			var i :int;
			var actionLength : int = actionArray.length;
			var tmpNode : ActionNode;
			var tmpBlock : Boolean;
			for(i = 0; i < actionLength; i++)
			{
				tmpNode = actionArray[i] as ActionNode;
//				tmpBlock = tmpNode.doAction(this, close_box2);
//				if(tmpBlock)
//					blockUser_ = tmpBlock;
			}
			
		}
		
		private function parseText(str:String):String
		{
			var myPattern:RegExp = /%nom%/g;
			var out : String = str;//.replace(myPattern,GameMain.user.name);
			myPattern = /\\n/g;
			out=out.replace(myPattern,"\n");
			var splitted : Array = out.split("%");
			if(splitted.length > 1)
			{
				var tmp:Array=splitted[1].split(":");
				if(tmp[0] == "sexe")
				{
					if(1) //GameMain.user.sexe)
						splitted[1]=tmp[1];
					else
						splitted[1]=tmp[2];
				}
				out=splitted.join("");		
			}
			return out;
		}
		
		private function onNextPage(e : NPCEvent) :void
		{
			parseDialogue();
		}
		
		private function onPlayerAnswerChoosed(e : NPCEvent) : void
		{
			_hasAnswers = false;
			//var answerNumber : int = e.answerNumber;
//			var actions : Array = _currentTextActions[answerNumber];
//			if(actions != null)
//			{
//				parseAction(actions);
//			}
//			var answerNode : INode = _currentAnswerBuffer[answerNumber];
//			_currentNode = answerNode.children[1];
//			_currentNodeChildIndex = 0;
//			_currentAnswerBuffer = new Vector.<INode>();
//			_dialogueView.clearPossibleAction();
			
			parseDialogue();
			
		}
	}
}